package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiangyi.teachingsystem.entity.po.PrivateRes;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.PrivateResService;
import com.xiangyi.teachingsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
public class PrivateResController {
    @Autowired
    PrivateResService privateResService;
    @Autowired
    UserService userService;

    @PostMapping("/uploadBatchPriRes")
    public Result uploadPriRes(@RequestBody List<PrivateRes> privateResList){
        synchronized (privateResList.getClass().getSimpleName()){
            try {
                HashMap<String, ArrayList<PrivateRes>> errMap = new HashMap<>();
                for (PrivateRes privateRes : privateResList) {
                    if (userService.getById(privateRes.getUserId())==null){
                        if (errMap.get("NotAllowUserId")==null){
                            ArrayList<PrivateRes> res = new ArrayList<>();
                            res.add(privateRes);
                            errMap.put("NotAllowUserId",res);
                        }else {
                            errMap.get("NotAllowUserId").add(privateRes);
                        }
                        continue;
                    }
                    PrivateRes resName = privateResService.getOne(new QueryWrapper<PrivateRes>()
                            .eq("res_name", privateRes.getResName())
                            .eq("user_id",privateRes.getUserId()));
                    if (resName!=null){
                        if (errMap.get("ExistName")==null){
                            ArrayList<PrivateRes> res = new ArrayList<>();
                            res.add(privateRes);
                            errMap.put("ExistName",res);
                        }else {
                            errMap.get("ExistName").add(privateRes);
                        }
                        continue;
                    }
                    privateResService.save(privateRes);
                }
                if (!errMap.isEmpty())
                    return Result.failure("部分资源未上传成功").setData(errMap);
                return Result.success("资源全部上传成功");
            }catch (Exception e){
                e.printStackTrace();
                return Result.failure("服务器错误").setData(e.getMessage());
            }
        }
    }

    @PostMapping("/updatePriRes")
    public Result updatePriRes(@Validated @RequestBody PrivateRes privateRes){
        try {
            PrivateRes byId = privateResService.getById(privateRes.getId());
            if (!Objects.equals(byId.getResName(), privateRes.getResName())){
                PrivateRes res_name = privateResService.getOne(new QueryWrapper<PrivateRes>()
                        .eq("res_name", privateRes.getResName()));
                if (res_name!=null)
                    return Result.failure("资源名称已存在");
            }
            privateResService.update(privateRes,new QueryWrapper<PrivateRes>()
                    .eq("id",privateRes.getId())
                    .eq("user_id",privateRes.getUserId()));
            return Result.success("资源信息更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @PostMapping("/delBatchPriRes")
    public Result delPriRes(@RequestBody List<Integer> idList){
        try {
            privateResService.removeByIds(idList);
            return Result.success("资源删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/getPriResList")
    public Result getPriResList(@RequestParam Integer userId){
        try {
            List<PrivateRes> priResList = privateResService.getPriResList(userId);
            return Result.success("个人资源获取成功").setData(priResList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/setPublic")
    public Result setPublic(@RequestParam Integer resId){
        try {
            PrivateRes privateRes = new PrivateRes();
            privateRes.setPublicSign(true);
            privateResService.update(privateRes,new QueryWrapper<PrivateRes>()
                    .eq("id",resId));
            return Result.success("设置公共资源成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/setPrivate")
    public Result setPrivate(@RequestParam Integer resId){
        try {
            PrivateRes privateRes = new PrivateRes();
            privateRes.setPublicSign(false);
            privateResService.update(privateRes,new QueryWrapper<PrivateRes>()
                    .eq("id",resId));
            return Result.success("设置私有资源成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/publicResList")
    public Result publicResList(){
        try {
            List<PrivateRes> publicResList = privateResService.publicResList();
            return Result.success("公共资源库查询成功").setData(publicResList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }


}
